<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
use kartik\file\FileInput;
use yii\helpers\Url;
use backend\models\EduEnter;

/* @var $this yii\web\View */
/* @var $model backend\models\EduEnter */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="edu-enter-form">

    <?php $form = ActiveForm::begin(['action'=> [''],'options' => ['enctype' => 'multipart/form-data']]); ?>
    <?php
    $pluginOptions = [
        // 需要预览的文件格式
        'previewFileType' => 'image',
        // 预览的文件
//            'initialPreview' => ['图片1', '图片2', '图片3'],
        // 需要展示的图片设置，比如图片的宽度等
//            'initialPreviewConfig' => ['width' => '120px'],
        // 是否展示预览图
        'initialPreviewAsData' => true,
        // 异步上传的接口地址设置
        'uploadUrl' => Url::toRoute(['upload/enter']),
        // 异步上传需要携带的其他参数，比如商品id等
//        'uploadExtraData' => [
//            'goods_id' => '111',
//        ],
        'uploadAsync' => true,
        // 最少上传的文件个数限制
        'minFileCount' => 1,
        // 最多上传的文件个数限制
        'maxFileCount' => 10,
        // 是否显示移除按钮，指input上面的移除按钮，非具体图片上的移除按钮
        'showRemove' => true,
        // 是否显示上传按钮，指input上面的上传按钮，非具体图片上的上传按钮
        'showUpload' => true,
        //是否显示[选择]按钮,指input上面的[选择]按钮,非具体图片上的上传按钮
        'showBrowse' => true,
        // 展示图片区域是否可点击选择多文件
        'browseOnZoneClick' => true,
        // 如果要设置具体图片上的移除、上传和展示按钮，需要设置该选项
        'fileActionSettings' => [
            // 设置具体图片的查看属性为false,默认为true
            'showZoom' => false,
            // 设置具体图片的上传属性为true,默认为true
            'showUpload' => true,
            // 设置具体图片的移除属性为true,默认为true
            'showRemove' => true,
        ]
    ];
    if ($model->image) {
//        $pluginOptions['initialPreview'] = Yii::$app->params['localUrl'].$model->image;
        $pluginOptions['initialPreview'] = $model->image;
    }
    echo $form->field($model, 'file')->label('海报上传')->widget(FileInput::classname(), [
        'options' => ['multiple' => false],
        'pluginOptions' => $pluginOptions,
        // 一些事件行为
        'pluginEvents' => [
            // 上传成功后的回调方法，需要的可查看data后再做具体操作，一般不需要设置
            "fileuploaded" => "function (event, data, id, index) {
            $('#eduenter-image').val(data['response']['message']);
        }",
        ],
    ]);
    ?>
    <?php ActiveForm::end(); ?>

    <?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>

    <?= $form->field($model, 'image')->textInput()->hiddenInput(['value' => $model->image]) ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'status')->dropDownList(EduEnter::allStatus()) ?>

    <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> 权益说明</h4></div>
            <div class="panel-body">
                <?php DynamicFormWidget::begin([
                    'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
                    'widgetBody' => '.container-items', // required: css class selector
                    'widgetItem' => '.item', // required: css class
                    'limit' => 10, // the maximum times, an element can be cloned (default 999)
                    'min' => 1, // 0 or 1 (default 1)
                    'insertButton' => '.add-item', // css class
                    'deleteButton' => '.remove-item', // css class
                    'model' => $modelsEquity[0],
                    'formId' => 'dynamic-form',
                    'formFields' => [
                        'equity',
                    ],
                ]); ?>

                <div class="container-items"><!-- widgetContainer -->
                    <?php foreach ($modelsEquity as $i => $modelEquity): ?>
                        <div class="item panel panel-default"><!-- widgetBody -->
                            <div class="panel-heading">
                                <h3 class="panel-title pull-left">权益条例</h3>
                                <div class="pull-right">
                                    <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                                    <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                                </div>
                                <div class="clearfix"></div>
                            </div>
                            <div class="panel-body">
                                <?php
                                // necessary for update action.
                                if (! $modelEquity->isNewRecord) {
                                    echo Html::activeHiddenInput($modelEquity, "[{$i}]id");
                                }
                                ?>
                                <div class="row">
                                    <div class="col-sm-12">
                                        <?= $form->field($modelEquity, "[{$i}]equity")->textInput(['maxlength' => true]) ?>
                                    </div>
                                </div><!-- .row -->
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <?php DynamicFormWidget::end(); ?>
            </div>
        </div>
    </div>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
